/**
 * worker.c
 * Author: Nikos Papakonstantinou (nikpapac@ics.forth.gr)
 */

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <assert.h>
#include "RCCE.h"
#include "PAPAC_header.h"
#include "header.h"

#define GET_PREVIOUS(x)	(x == 1 ? x= RCCE_num_ues() : --x)

/**
 * void worker();
 *
 * Recieving Task descriptor from MASTER
 * Check if task.extras in number of ue
 * Recieving the arguments if the number of ue is equal with extras
 * Execute the task
 * Flush shared memory and L2 Chache of core for coherency
 *
 */
void worker(){
	int i;
	int UE = iam;
	tpc_task_descriptor tpc_task;
	tpc_task_argument tmp_arg,*tpc_task_args;

	RCCE_recv((char *)&tpc_task, sizeof(tpc_task_descriptor), MASTER);

	if((int)tpc_task.extras == iam){
		tpc_task_args = (tpc_task_argument *)malloc(tpc_task.args_num*sizeof(tpc_task_argument));
		for(i=0;i<tpc_task.args_num;i++){
			RCCE_recv((char *)&tmp_arg, sizeof(tpc_task_argument), MASTER);
// 			printf("%d\n",tmp_arg);
			tpc_task_args[i] = tmp_arg;
		}
		RCCE_acquire_lock(0);
		tpc_task.task(tpc_task_args);
		RCCE_DCMflush();//L2 flush
		RCCE_release_lock(0);
		free(tpc_task_args);
	}
}
